﻿using System;
using System.Collections.Generic;

namespace ShapeProgram
{
    public abstract class Shape
    {
        public abstract bool IsValid { get; } // 判断形状是否合法
        public abstract double Area { get; }  // 计算面积
    }

    // 长方形类
    public class Rectangle : Shape
    {
        public double Length { get; set; }
        public double Width { get; set; }

        public Rectangle(double length, double width)
        {
            Length = length;
            Width = width;
        }

        public override bool IsValid
        {
            get
            {
               return Length > 0 && Width > 0;
            }
        }


        public override double Area
        {
            get { return IsValid ? Length * Width : 0; }
        }
    }

    // 正方形类
    public class Square : Shape
    {
        public double Side { get; set; }

        public Square(double side)
        {
            Side = side;
        }

        public override bool IsValid => Side > 0;

        public override double Area => IsValid ? Side * Side : 0;
    }

    // 圆形类
    public class Circle : Shape
    {
        public double Radius { get; set; }

        public Circle(double radius)
        {
            Radius = radius;
        }

        public override bool IsValid => Radius > 0;

        public override double Area => IsValid ? Math.PI * Radius * Radius : 0;
    }

    class Program
    {
        static void Main(string[] args)
        {
            // 随机创建10个形状对象
            Random random = new Random();
            List<Shape> shapes = new List<Shape>();

            for (int i = 0; i < 10; i++)
            {
                // 随机选择形状类型
                int shapeType = random.Next(3); // 0: Rectangle, 1: Square, 2: Circle

                switch (shapeType)
                {
                    case 0:
                        double length = random.Next(1, 10);
                        double width = random.Next(1, 10);
                        shapes.Add(new Rectangle(length, width));
                        break;
                    case 1:
                        double side = random.Next(1, 10);
                        shapes.Add(new Square(side));
                        break;
                    case 2:
                        double radius = random.Next(1, 10);
                        shapes.Add(new Circle(radius));
                        break;
                }
            }

            // 计算所有形状的面积之和
            double totalArea = 0;
            foreach (var shape in shapes)
            {
                if (shape.IsValid)
                {
                    totalArea += shape.Area;
                }
            }

            // 输出结果
            Console.WriteLine("随机创建的10个形状的面积之和为：" + totalArea);
        }
    }
}